/**
 * 38. 外观数列
 */
class Solution1 {
    public String countAndSay(int n) {
        if(n == 1) return "1";
        String ret = "1";
        for(int i = 2; i <= n; i++) {
            ret = generateNext(ret);
        }
        return ret;
    }

    public String generateNext(String cur) {
        int count = 1;
        StringBuilder next = new StringBuilder();
        char c = cur.charAt(0);
        for(int i = 1; i < cur.length(); i++) {
            if (c == cur.charAt(i)) {
                count++;
            } else {
                // 遇到不同的了
                next.append(count).append(c);
                count = 1;
                c = cur.charAt(i);
            }
        }
        // 处理最后一组数据
        next.append(count).append(c);
        return new String(next);
    }
}